package com.gxa.clientorder.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.clientorder.exception.BusinessException;
import com.gxa.clientorder.pojo.Order;
import com.gxa.clientorder.service.GroupbuyService;
import com.gxa.clientorder.service.OrderService;
import com.gxa.clientorder.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author FanJiang
 * @Date 2020/11/12
 * @Version 1.0
 */
@RestController
@RequestMapping("/ord")
@Api(value = "执行订单操作的控制器，包含前后台",tags = "订单接口")
public class OrderController {

    @Resource
    OrderService orderService;
    @Resource
    GroupbuyService groupbuyService;

    /**
     * 订单列表接口
     *
     * @param page  当前页码
     * @param limit 页面显示数据条数
     * @param ordnum 订单编号
     * @param addname 收货人
     * @param state 订单状态
     * @return json
     */
    @ApiOperation(value="获取订单列表", notes="订单列表页面，分页显示数据接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页显示数量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "state", value = "订单状态", required = true, dataType = "int"),
            @ApiImplicitParam(name = "ordnum", value = "订单号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "username", value = "收货人", required = true, dataType = "string")
    })
    //权限注解
    @RequiresPermissions("12")
    @PostMapping("/list")
    public Map list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                    @RequestParam("ordnum") String ordnum,@RequestParam("addname") String addname,
                    @RequestParam("state") Integer state) {
        PageHelper.startPage(page,limit);
        List list = orderService.orderList(ordnum, addname, state);
        PageInfo info = PageInfo.of(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(info.getList());
        jsonResult.put("count",info.getTotal());
        return jsonResult.getValues();
    }

    /**
     * 更新支付状态，由支付接口负责调用
     * @param ordnum 订单编号
     * @param state 支付状态，0未支付，1已支付,3/已取消
     * @return
     */
    @PostMapping("/paystate")
    public Map updatepaystate(@RequestParam("ordnum") String ordnum,@RequestParam("state") Integer state){
        Integer integer = orderService.updatePay(ordnum, state);
        if(integer == 0){
            throw new BusinessException("失败啦o(╥﹏╥)o");
        }
        return null;
    }

    /**
     * 用户取消订单使用的接口
     * @param ordnum 订单编号
     * @param state 支付状态，0未支付，1已支付,3/已取消
     * @return
     */
    @ApiOperation(value="取消订单接口", notes="前台用户取消订单则调用此接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordnum", value = "取消订单的订单号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "state", value = "状态，请传4拼团失败，否则无法执行", required = true, dataType = "int")
    })
    @PostMapping("/offpay")
    public Map offpay(@RequestParam("ordnum") String ordnum,@RequestParam("state") Integer state){
        //判断返回的状态是否为3（取消订单）。只允许用户执行取消订单的操作
        if (state != 3){
            throw new BusinessException("用户只能选择取订单哦，不能更改其他状态呢");
        }
        //执行更新
        Integer integer = orderService.updatePay(ordnum, state);
        //判断是否更新数据库数据
        if(integer == 0){
            throw new BusinessException("失败啦o(╥﹏╥)o");
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("取消订单成功");
        return jsonResult.getValues();
    }

    /**
     * 秒杀接口
     *
     * @param userid   下单用户id
     * @param sysnum   商品编号
     * @param username 下单用户昵称
     * @param sendtype 配送方式
     * @param usermeg  用户留言
     * @return
     */
    @ApiOperation(value="秒杀接口", notes="前台用户秒杀则调用此接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "当前登录用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sysnum", value = "购买商品系统编号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "username", value = "当前登录用户昵称", required = true, dataType = "string"),
            @ApiImplicitParam(name = "usermeg", value = "用户留言", required = true, dataType = "string"),
            @ApiImplicitParam(name = "sendtype", value = "配送方式(1、快递配送，2、门店自提)", required = true, dataType = "int"),
            @ApiImplicitParam(name = "money", value = "订单金额(double类型)", required = true, dataType = "double")
    })
    @PostMapping("/kill")
    public Map killOrd(@RequestParam("userid") Integer userid, @RequestParam("sysnum") String sysnum,
                       @RequestParam("username") String username, @RequestParam("sendtype") Integer sendtype,
                       @RequestParam("usermeg") String usermeg,@RequestParam("money") Double money) {
        Order order = new Order();
        order.setUserid(userid);
        order.setUsername(username);
        order.setSysnum(sysnum);
        order.setSendtype(sendtype);
        order.setMessage(usermeg);
        order.setOrdtype(2);
        order.setMoney(money);
        String addord = orderService.insert(order);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("ordnum",addord);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    /**
     * 抽奖接口
     * @param userid   下单用户id
     * @param sysnum   商品编号
     * @param username 下单用户昵称
     * @param sendtype 配送方式
     * @param usermeg  用户留言
     * @return
     */
    @ApiOperation(value="抽奖接口", notes="前台用户抽奖则调用此接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "当前登录用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sysnum", value = "购买商品系统编号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "username", value = "当前登录用户昵称", required = true, dataType = "string"),
            @ApiImplicitParam(name = "usermeg", value = "用户留言", required = true, dataType = "string"),
            @ApiImplicitParam(name = "sendtype", value = "配送方式(1、快递配送，2、门店自提)", required = true, dataType = "int"),
            @ApiImplicitParam(name = "money", value = "订单金额(double类型)", required = true, dataType = "double")
    })
    @PostMapping("/luck")
    public Map luckOrd(@RequestParam("userid") Integer userid, @RequestParam("sysnum") String sysnum,
                       @RequestParam("username") String username, @RequestParam("sendtype") Integer sendtype,
                       @RequestParam("usermeg") String usermeg,@RequestParam("money") Double money) {

        Order order = new Order();
        order.setUserid(userid);
        order.setUsername(username);
        order.setSysnum(sysnum);
        order.setSendtype(sendtype);
        order.setMessage(usermeg);
        order.setOrdtype(3);
        order.setMoney(money);
        String addord = orderService.insert(order);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("ordnum",addord);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    /**
     * 助力接口
     * @param userid   下单用户id
     * @param sysnum   商品编号
     * @param username 下单用户昵称
     * @param sendtype 配送方式
     * @param usermeg  用户留言
     * @return
     */
    @ApiOperation(value="助力接口", notes="前台用户助力则调用此接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "当前登录用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sysnum", value = "购买商品系统编号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "username", value = "当前登录用户昵称", required = true, dataType = "string"),
            @ApiImplicitParam(name = "usermeg", value = "用户留言", required = true, dataType = "string"),
            @ApiImplicitParam(name = "sendtype", value = "配送方式(1、快递配送，2、门店自提)", required = true, dataType = "int"),
            @ApiImplicitParam(name = "money", value = "订单金额(double类型)", required = true, dataType = "double")
    })
    @PostMapping("/help")
    public Map helpOrd(@RequestParam("userid") Integer userid, @RequestParam("sysnum") String sysnum,
                       @RequestParam("username") String username, @RequestParam("sendtype") Integer sendtype,
                       @RequestParam("usermeg") String usermeg,@RequestParam("money") Double money) {

        Order order = new Order();
        order.setUserid(userid);
        order.setUsername(username);
        order.setSysnum(sysnum);
        order.setSendtype(sendtype);
        order.setMessage(usermeg);
        order.setOrdtype(4);
        order.setMoney(money);
        String addord = orderService.insert(order);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("ordnum",addord);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    /**
     * 普通订单接口
     *
     * @param userid   下单用户id
     * @param sysnum   商品编号
     * @param username 下单用户昵称
     * @param sendtype 配送方式
     * @param usermeg  用户留言
     * @return
     */
    @ApiOperation(value="普通订单接口", notes="前台用户下单普通订单则调用此接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "当前登录用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sysnum", value = "购买商品系统编号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "username", value = "当前登录用户昵称", required = true, dataType = "string"),
            @ApiImplicitParam(name = "usermeg", value = "用户留言", required = true, dataType = "string"),
            @ApiImplicitParam(name = "sendtype", value = "配送方式(1、快递配送，2、门店自提)", required = true, dataType = "int"),
            @ApiImplicitParam(name = "money", value = "订单金额(double类型)", required = true, dataType = "double")
    })
    @PostMapping("/gen")
    public Map genOrd(@RequestParam("userid") Integer userid, @RequestParam("sysnum") String sysnum,
                       @RequestParam("username") String username, @RequestParam("sendtype") Integer sendtype,
                       @RequestParam("usermeg") String usermeg,@RequestParam("money") Double money) {

        Order order = new Order();
        order.setUserid(userid);
        order.setUsername(username);
        order.setSysnum(sysnum);
        order.setSendtype(sendtype);
        order.setMessage(usermeg);
        order.setOrdtype(5);
        order.setMoney(money);
        String addord = orderService.insert(order);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("ordnum",addord);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    /**
     * 我的订单接口
     * @param userid 用户id
     * @param zstate 订单综合状态
     * 0/待付款、1/待发货、2/待收货、
     * 3/待核销、4/待评价、5/已完成、
     * 6/已取消、7/已退款、8/无效、10、全部
     * @return
     */
    @ApiOperation(value="我的订单接口", notes="前台用户点击我的订单则调用此接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "当前登录用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "zstate", value = "订单综合状态，0/待付款、1/待发货、2/待收货、3/待核销、4/待评价、5/已完成、6/已取消、7/已退款、8/无效、10、全部", required = true, dataType = "int")
    })
    @PostMapping("/userord")
    public Map userOrd(@RequestParam("userid") Integer userid,@RequestParam("username") String username,
                       @RequestParam("zstate") Integer zstate){
        Order order = new Order();
        order.setUserid(userid);
        order.setUsername(username);
        //判断式查询全部还是分类
        if (zstate != null && zstate != 10){
            order.setZstate(zstate);
        }
        System.out.println(order);
        List byuserid = orderService.findByuserid(order);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(byuserid);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    /**
     * 订单详情接口
     * @param ordnum 订单编号
     * @return
     */
    @ApiOperation(value="订单详情接口", notes="点击订单详情则调用此接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordnum", value = "订单编号", required = true, dataType = "string")})
    @PostMapping("/detail")
    public Map userOrddetail(@RequestParam("ordnum") String ordnum){

        Order byOrdnum = orderService.findByOrdnum(ordnum);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(byOrdnum);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    /**
     * 订单核销接口
     * @param ordnum 订单号
     * @param zstate 综合状态
     * @return
     */
    @ApiOperation(value="订单核销接口", notes="后台核销订单则调用此接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordnum", value = "订单编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "zstate", value = "订单综合状态，请传3（待核销）", required = true, dataType = "int")
    })
    @PostMapping("/useord")
    public Map userOrd(@RequestParam("ordnum") String ordnum,@RequestParam("zstate") Integer zstate){

        if (zstate != 3){
            throw new BusinessException("只有待核销的订单才能被核销哦");
        }
        Integer integer = orderService.userOrd(ordnum);
        if (integer == 0){
            throw new BusinessException("核销失败");
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

}
